package com.freshfresh.view;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class BannerPager extends ViewPager
{
  private boolean canScroll;
  private long mScrollTime;
  private Timer mTimer;

  public BannerPager(Context paramContext)
  {
    super(paramContext);
    initParams();
  }

  public BannerPager(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    initParams();
  }

  private void initParams()
  {
    this.mScrollTime = 3000L;
    this.canScroll = true;
  }

  public long getmScrollTime()
  {
    return this.mScrollTime;
  }

  public boolean isCanScroll()
  {
    return this.canScroll;
  }

  public void setCanScroll(boolean paramBoolean)
  {
    this.canScroll = paramBoolean;
  }

  @SuppressLint({"ClickableViewAccessibility"})
  public void setOvalLayout(Activity paramActivity, final ViewGroup paramViewGroup, final int paramInt1, int paramInt2)
  {
    if ((paramViewGroup == null) || (paramInt1 == 0))
      return;
    paramViewGroup.removeAllViews();
    LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(30, 20);
    for (int i = 0; ; i++)
    {
      if (i >= paramInt1)
      {
        paramViewGroup.getChildAt(0).setSelected(true);
        setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
          public void onPageScrollStateChanged(int paramAnonymousInt)
          {
          }

          public void onPageScrolled(int paramAnonymousInt1, float paramAnonymousFloat, int paramAnonymousInt2)
          {
          }

          public void onPageSelected(int paramAnonymousInt)
          {
            for (int i = 0; ; i++)
            {
              if (i >= paramInt1)
              {
                paramViewGroup.getChildAt(paramAnonymousInt % paramInt1).setSelected(true);
                return;
              }
              paramViewGroup.getChildAt(i).setSelected(false);
            }
          }
        });
        setOnTouchListener(new View.OnTouchListener()
        {
          public boolean onTouch(View paramAnonymousView, MotionEvent paramAnonymousMotionEvent)
          {
            if (!BannerPager.this.canScroll)
              return false;
            paramAnonymousMotionEvent.getAction();
            return false;
          }
        });
        return;
      }
      ImageView localImageView = new ImageView(paramActivity);
      localImageView.setPadding(5, 0, 5, 0);
      localImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      localImageView.setLayoutParams(localLayoutParams);
      localImageView.setImageResource(paramInt2);
      localImageView.setSelected(false);
      paramViewGroup.addView(localImageView);
    }
  }

  public void setmScrollTime(long paramLong)
  {
    this.mScrollTime = paramLong;
  }

  public void startScroll(final Activity paramActivity)
  {
    if (!isCanScroll())
      return;
    this.mTimer = new Timer();
    this.mTimer.schedule(new TimerTask()
    {
      public void run()
      {
        paramActivity.runOnUiThread(new Runnable()
        {
          public void run()
          {
            BannerPager.this.setCurrentItem(1 + BannerPager.this.getCurrentItem());
          }
        });
      }
    }
    , this.mScrollTime, this.mScrollTime);
  }

  public void stopScroll()
  {
    if (this.mTimer != null)
    {
      this.mTimer.cancel();
      this.mTimer = null;
    }
  }
}